package com.okyai.qa.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.okyai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.okyai.qa.domain.NotesRecord;
import com.okyai.qa.mapper.NotesMapper;
import com.okyai.qa.domain.Notes;
import com.okyai.qa.service.INotesService;
import com.okyai.common.core.text.Convert;

/**
 * 答题记录Service业务层处理
 * 
 * @author okyai
 * @date 2021-03-19
 */
@Service
public class NotesServiceImpl implements INotesService 
{
    @Autowired
    private NotesMapper notesMapper;

    /**
     * 查询答题记录
     * 
     * @param notesId 答题记录ID
     * @return 答题记录
     */
    @Override
    public Notes selectNotesById(Long notesId)
    {
        return notesMapper.selectNotesById(notesId);
    }

    /**
     * 查询答题记录列表
     * 
     * @param notes 答题记录
     * @return 答题记录
     */
    @Override
    public List<Notes> selectNotesList(Notes notes)
    {
        return notesMapper.selectNotesList(notes);
    }

    /**
     * 新增答题记录
     * 
     * @param notes 答题记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNotes(Notes notes)
    {
        int rows = notesMapper.insertNotes(notes);
        insertNotesRecord(notes);
        return rows;
    }

    /**
     * 修改答题记录
     * 
     * @param notes 答题记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNotes(Notes notes)
    {
        notesMapper.deleteNotesRecordByNotesId(notes.getNotesId());
        insertNotesRecord(notes);
        return notesMapper.updateNotes(notes);
    }

    /**
     * 删除答题记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNotesByIds(String ids)
    {
        notesMapper.deleteNotesRecordByNotesIds(Convert.toStrArray(ids));
        return notesMapper.deleteNotesByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除答题记录信息
     * 
     * @param notesId 答题记录ID
     * @return 结果
     */
    @Override
    public int deleteNotesById(Long notesId)
    {
        notesMapper.deleteNotesRecordByNotesId(notesId);
        return notesMapper.deleteNotesById(notesId);
    }

    /**
     * 新增答题记录项目信息
     * 
     * @param notes 答题记录对象
     */
    public void insertNotesRecord(Notes notes)
    {
        List<NotesRecord> notesRecordList = notes.getNotesRecordList();
        Long notesId = notes.getNotesId();
        if (StringUtils.isNotNull(notesRecordList))
        {
            List<NotesRecord> list = new ArrayList<NotesRecord>();
            for (NotesRecord notesRecord : notesRecordList)
            {
                notesRecord.setNotesId(notesId);
                list.add(notesRecord);
            }
            if (list.size() > 0)
            {
                notesMapper.batchNotesRecord(list);
            }
        }
    }

	@Override
	public Notes answer(Notes notes, Map<String, String[]> map) {
		notesMapper.insertNotes(notes);
		List<NotesRecord> list = new ArrayList<NotesRecord>();
		AtomicLong questionnaireId = new AtomicLong(0);
		map.forEach((k,v) ->{
			if(k.equals("detailId")||k.equals("recordId")) {
				return;
			}
            for(String value:v) {
            	NotesRecord nr = new NotesRecord();
            	nr.setNotesId(notes.getNotesId());
            	nr.setAnswerName(k);
            	nr.setAnswerValue(value);
            	String[] s= k.split("_");
            	questionnaireId.set(Long.parseLong(s[1])); ;
            	nr.setQuestionsId(Long.parseLong(s[2]));
            	list.add(nr);
           	 	System.out.println(k +","+ value);
            }
        });
		notes.setQuestionnaireId(questionnaireId.get());
		notesMapper.updateNotes(notes);
		notesMapper.batchNotesRecord(list);
		return notes;
	}
    
    
}
